<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/9/26
 * Time: 17:24
 */

namespace app\admin\controller;


use app\common\model\AnswerMethod;
use think\Lang;
use app\common\model\Goodsclass;
use think\Request;

class Answer extends AdminControl
{
    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/answer.lang.php');
    }

    public function index(){
        $answer = new \app\common\model\Answer();
        $condition = [];
        $search_content = input('get.search_content', '');
        if ($search_content) {
            $condition['content'] = array('like', "%" . $search_content . "%");
        }
        $answer_list = $answer->getAnswerList($condition,10);
        $this->assign('answer_list',$answer_list);
//        var_dump($answer_list);die;

        $this->assign('show_page', $answer->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    public function edit(Request $request){
        $id = intval(input('answer_id', '', 'intval'));
        if (empty($id)) {
            $this->error(lang('param_error'));
        }
        $answer = \app\common\model\Answer::find($id);
        if (empty($answer)) {
            $this->error(lang('param_error'));
        }
        if (!$request->isPost()) {
            $this->assign('answer', $answer);
            return $this->fetch('edit');
        }
        $content = input('post.content', '');
        $answer->content = $content;
        if($answer->save()){
            dsLayerOpenSuccess(lang('answer_edit_succ'));
        }else{
            $this->error(lang('answer_edit_fail'));
        }
    }

    public function add(Request $request){
        $answer = new \app\common\model\Answer();
        if($request->isPost()){
            $data['content'] = input('param.content');
            if(!$data['content']){
                $this->error(lang('内容不能为空'));

            }
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $an_id = $answer->addAnswer($data);

            if($an_id){
                dsLayerOpenSuccess('添加成功');
            }else{
                $this->error(lang('添加失败'));
            }
        }else{
            $class = new Goodsclass();

            $classInfo = $class->getTreeClassList();


            $this->setAdminCurItem('add');
            return $this->fetch();
        }

    }

    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' =>lang('ds_manage'),
                'url' => url('answer/index')
            ),

        );
        /*if (request()->action() == 'add' || request()->action() == 'index') {
            $menu_array[] = array(
                'name' => 'add',
                'text' => lang('ds_new'),
                'url' =>url("answer/add"),
            );
        }*/
        if (request()->action() == 'edit') {
            $menu_array[] = array(
                'name' => 'edit',
                'text' => lang('ds_edit'),
                'url' => 'javascript:void(0)'
            );
        }
        return $menu_array;
    }

    public function delete()
    {
        $id = input('get.id', '', 'intval');
        // 判断该答案是否关联了选项
        $model = \app\common\model\Answer::find($id);
        if ($model->option_id) {
            return json_encode(['code' => 0, 'message' => '请先解除关联后删除']);
        }
        if (\app\common\model\Answer::destroy($id)) {
            return json_encode(['code' => 10000, 'message' => '删除成功']);
        } else {
            return json_encode(['code' => 0, 'message' => '删除失败']);
        }
    }

    public function addMethod(Request $request)
    {
        $id = input('get.id', '', 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        if(!$request->isPost()){
            // 获取所有当前答案下的处理方法
            $method_list = \app\common\model\Answer::getMethodList($id);
            $this->assign('answerid', $id);
            $this->assign('method_list', $method_list);
            return $this->fetch('add-method');
        }
        $content = input('post.content');

        $method = new AnswerMethod();
        $method->content = $content;
        $method->created_at = date('Y-m-d H:i:s');
        $method->updated_at = date('Y-m-d H:i:s');
        if (!$method->save()) return json_encode(['code' => 0, 'message' => '添加失败']);
        $method_id = $method->id;
        if (!\app\common\model\Answer::addMethod($id, $method_id)) {
            return json_encode(['code' => 0, 'message' => '关联失败']);
        }

        return json_encode(['code' => 10000, 'message' => '添加成功', 'id' => $method_id]);


    }

    /**
     * 解除关联
     * @return string
     */
    public function deleteMethod()
    {
        $answer_id = input('get.answer_id');
        $method_id = input('get.method_id');
        if (!\app\common\model\Answer::deleteMethod($answer_id, $method_id)) {
            return json_encode(['code' => 0, 'message' => '删除失败']);
        }
        return json_encode(['code' => 10000, 'message' => '删除成功']);

    }
}
